package nl.hva.web.mvcwebapp.controllers;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import nl.hva.web.mvcwebapp.models.Course;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import nl.hva.web.mvcwebapp.doa.ChapterDAO;
import nl.hva.web.mvcwebapp.doa.CourseDAO;
import nl.hva.web.mvcwebapp.doa.UserDAO;
import nl.hva.web.mvcwebapp.models.Chapter;
import nl.hva.web.mvcwebapp.models.News;
import nl.hva.web.mvcwebapp.models.User;

public class ChapterController extends HttpServlet {

    private String address = "/chapters.jsp";
    ArrayList paths_jsp = new ArrayList();
    ArrayList paths = new ArrayList();

    /*
     * HTTP GET request
     */
    public ArrayList getPaths() {
        ArrayList paths = new ArrayList();
        paths.add(0, "/course/chapters");
        paths.add(1, "/chapter/view");
        paths.add(2, "/chapter/edit");
        paths.add(3, "/chapter/new");
        paths.add(4, "/chapter/delete");
        return paths;
    }

    public ArrayList getPathsJSP() {
        ArrayList paths_jsp = new ArrayList();
        paths_jsp.add(0, "/chapters/chapters.jsp");
        paths_jsp.add(1, "/chapters/chapter_view.jsp");
        paths_jsp.add(2, "/chapters/chapter_edit.jsp");
        paths_jsp.add(3, "/chapters/chapter_edit.jsp");
        paths_jsp.add(4, "/chapters/chapters.jsp");


        return paths_jsp;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        boolean redirect = false;
        ArrayList paths = getPaths();
        ArrayList paths_jsp = getPathsJSP();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        String path = request.getServletPath();


        for (int i = 0; i < paths.size(); i++) {
            try {
                if (path.equals(paths.get(i).toString())
                        || path.substring(0, path.toString().length() - 1).equals(paths.get(i).toString())) {
                    // /course/chapters
                    System.out.println(i);
                    if (i == 0) {
                        System.out.println("chapters");
                        int courseid = Integer.parseInt(request.getParameter("courseid"));
                        Course course = CourseDAO.getCourse(courseid);
                        List<Chapter> chapterlist = ChapterDAO.getAllChapters(course.getCourseid());
                        request.setAttribute("course", course);
                        request.setAttribute("chapters", chapterlist);
                        request.setAttribute("amountchapters", chapterlist.size());
                    } // /chapter/view
                    else if (i == 1) {
                        int chapterid = Integer.parseInt(request.getParameter("chapterid"));
                        Chapter chapter = ChapterDAO.getChapter(chapterid);
                        request.setAttribute("chapter", chapter);
                    }// /chapter/edit
                    else if (i == 2) {
                        int id = Integer.parseInt(request.getParameter("chapterid"));
                        Chapter chapter = ChapterDAO.getChapter(id);
                        Course course = chapter.getCourse();
                        request.setAttribute("course", course);
                        request.setAttribute("chapter", chapter);
                        request.setAttribute("pageTitle", "Edit Chapter");
                        request.setAttribute("buttonTitle", "Edit");
                    }// /chapter/new
                    else if (i == 3) {
                        int id = Integer.parseInt(request.getParameter("courseid"));
                        Course course = CourseDAO.getCourse(id);
                        request.setAttribute("course", course);
                        request.setAttribute("pageTitle", "New Chapter");
                        request.setAttribute("buttonTitle", "Add");
                    }// /chapter/delete
                    else if (i == 4) {
                        System.out.println("system");
                        int chapterid = Integer.parseInt(request.getParameter("chapterid"));
                        int courseid = Integer.parseInt(request.getParameter("courseid"));
                        ChapterDAO.deleteChapter(chapterid);
                        Course course = CourseDAO.getCourse(courseid);
                        redirect = true;
                        System.out.println("delete");
                        response.sendRedirect(response.encodeRedirectURL("../course/chapters?courseid="+course.getCourseid()));
                    }
                    address = paths_jsp.get(i).toString();
                }
            } catch (Exception E) {
            }
        }

        request.setAttribute("path", path);
        if(!redirect){
            RequestDispatcher dispatcher = request.getRequestDispatcher(address);
            dispatcher.forward(request, response);
        }
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        ArrayList paths = getPaths();
        ArrayList paths_jsp = getPathsJSP();
       // Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        //Transaction tx = session.beginTransaction();
        String path = request.getServletPath();
        for (int i = 0; i < paths.size(); i++) {
            try {
                if (path.equals(paths.get(i).toString())
                        || path.substring(0, path.toString().length() - 1).equals(paths.get(i).toString())) {
                    address = paths_jsp.get(i).toString();
                    // ---------------- /chapter/edit --------------------
                    if (i == 2) {
                        int courseid = Integer.parseInt(request.getParameter("courseid"));
                        Course course = CourseDAO.getCourse(courseid);
                        ChapterDAO.updateChapter(request, course);
                        response.sendRedirect(response.encodeRedirectURL("../course/chapters?courseid="+course.getCourseid()));
                    }// ----------) {------ /chapter/new --------------------
                    else if (i == 3) {
                        int courseid = Integer.parseInt(request.getParameter("courseid"));
                        Course course = CourseDAO.getCourse(courseid);
                        ChapterDAO.insertChapter(request, course);
                        response.sendRedirect(response.encodeRedirectURL("../course/chapters?courseid="+courseid));
                    }
                    
                }


            } catch (Exception E) {
            }
        }

    }
}
